{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ca3c0317-6bcd-45f4-a162-b2a352284fda",
   "metadata": {},
   "source": [
    "# 第三节、文件IO操作"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "45170353-c1b3-4e16-94c1-cfb6f89beae7",
   "metadata": {},
   "source": [
    "## 保存数据"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ecc7365-836f-449f-ab7a-4e9ce7816295",
   "metadata": {},
   "source": [
    "save方法报错ndarray到一个npy文件，也可以使用savez将多个ndarray保存到同一个.nyz文件中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8c9bad8b-9fb3-4d81-9725-1d3ee356d686",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "baaa27ba-2fbc-4dce-bb40-22e75dc5c144",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  1.        ,   3.02040816,   5.04081633,   7.06122449,\n",
       "          9.08163265,  11.10204082,  13.12244898,  15.14285714,\n",
       "         17.16326531,  19.18367347],\n",
       "       [ 21.20408163,  23.2244898 ,  25.24489796,  27.26530612,\n",
       "         29.28571429,  31.30612245,  33.32653061,  35.34693878,\n",
       "         37.36734694,  39.3877551 ],\n",
       "       [ 41.40816327,  43.42857143,  45.44897959,  47.46938776,\n",
       "         49.48979592,  51.51020408,  53.53061224,  55.55102041,\n",
       "         57.57142857,  59.59183673],\n",
       "       [ 61.6122449 ,  63.63265306,  65.65306122,  67.67346939,\n",
       "         69.69387755,  71.71428571,  73.73469388,  75.75510204,\n",
       "         77.7755102 ,  79.79591837],\n",
       "       [ 81.81632653,  83.83673469,  85.85714286,  87.87755102,\n",
       "         89.89795918,  91.91836735,  93.93877551,  95.95918367,\n",
       "         97.97959184, 100.        ]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n1 = np.linspace(1, 100, num=50).reshape(5, 10)\n",
    "n1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6542281e-10df-4fbd-9656-19d47ab16531",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 通过save方法保存一个ndarray\n",
    "np.save('data_n1.npy', n1)  # 第一个参数是文件名字，第二个参数是要保存的ndarray"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "3d62a716-f865-44c2-b000-6a12242825d9",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122,\n",
       "         124, 126, 128, 130, 132, 134, 136, 138, 140, 142, 144, 146,\n",
       "         148, 150, 152, 154, 156, 158],\n",
       "        [160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180, 182,\n",
       "         184, 186, 188, 190, 192, 194, 196, 198, 200, 202, 204, 206,\n",
       "         208, 210, 212, 214, 216, 218],\n",
       "        [220, 222, 224, 226, 228, 230, 232, 234, 236, 238, 240, 242,\n",
       "         244, 246, 248, 250, 252, 254, 256, 258, 260, 262, 264, 266,\n",
       "         268, 270, 272, 274, 276, 278],\n",
       "        [280, 282, 284, 286, 288, 290, 292, 294, 296, 298, 300, 302,\n",
       "         304, 306, 308, 310, 312, 314, 316, 318, 320, 322, 324, 326,\n",
       "         328, 330, 332, 334, 336, 338],\n",
       "        [340, 342, 344, 346, 348, 350, 352, 354, 356, 358, 360, 362,\n",
       "         364, 366, 368, 370, 372, 374, 376, 378, 380, 382, 384, 386,\n",
       "         388, 390, 392, 394, 396, 398]],\n",
       "\n",
       "       [[400, 402, 404, 406, 408, 410, 412, 414, 416, 418, 420, 422,\n",
       "         424, 426, 428, 430, 432, 434, 436, 438, 440, 442, 444, 446,\n",
       "         448, 450, 452, 454, 456, 458],\n",
       "        [460, 462, 464, 466, 468, 470, 472, 474, 476, 478, 480, 482,\n",
       "         484, 486, 488, 490, 492, 494, 496, 498, 500, 502, 504, 506,\n",
       "         508, 510, 512, 514, 516, 518],\n",
       "        [520, 522, 524, 526, 528, 530, 532, 534, 536, 538, 540, 542,\n",
       "         544, 546, 548, 550, 552, 554, 556, 558, 560, 562, 564, 566,\n",
       "         568, 570, 572, 574, 576, 578],\n",
       "        [580, 582, 584, 586, 588, 590, 592, 594, 596, 598, 600, 602,\n",
       "         604, 606, 608, 610, 612, 614, 616, 618, 620, 622, 624, 626,\n",
       "         628, 630, 632, 634, 636, 638],\n",
       "        [640, 642, 644, 646, 648, 650, 652, 654, 656, 658, 660, 662,\n",
       "         664, 666, 668, 670, 672, 674, 676, 678, 680, 682, 684, 686,\n",
       "         688, 690, 692, 694, 696, 698]],\n",
       "\n",
       "       [[700, 702, 704, 706, 708, 710, 712, 714, 716, 718, 720, 722,\n",
       "         724, 726, 728, 730, 732, 734, 736, 738, 740, 742, 744, 746,\n",
       "         748, 750, 752, 754, 756, 758],\n",
       "        [760, 762, 764, 766, 768, 770, 772, 774, 776, 778, 780, 782,\n",
       "         784, 786, 788, 790, 792, 794, 796, 798, 800, 802, 804, 806,\n",
       "         808, 810, 812, 814, 816, 818],\n",
       "        [820, 822, 824, 826, 828, 830, 832, 834, 836, 838, 840, 842,\n",
       "         844, 846, 848, 850, 852, 854, 856, 858, 860, 862, 864, 866,\n",
       "         868, 870, 872, 874, 876, 878],\n",
       "        [880, 882, 884, 886, 888, 890, 892, 894, 896, 898, 900, 902,\n",
       "         904, 906, 908, 910, 912, 914, 916, 918, 920, 922, 924, 926,\n",
       "         928, 930, 932, 934, 936, 938],\n",
       "        [940, 942, 944, 946, 948, 950, 952, 954, 956, 958, 960, 962,\n",
       "         964, 966, 968, 970, 972, 974, 976, 978, 980, 982, 984, 986,\n",
       "         988, 990, 992, 994, 996, 998]]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n2 = np.arange(100, 1000, 2).reshape(3, 5, -1)\n",
    "n2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6c2cebda-9bdb-4907-a9f6-2e921cbac171",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 通过savez方法一次性保存多个ndarray\n",
    "np.savez('data_zip_n1_n2.npz', n1=n1, n2=n2)  # 按照键值对的形式传参，这样取值的时候也可以按照键来取值j"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef700bcd-579b-45a2-859e-a0aa93105e43",
   "metadata": {},
   "source": [
    "## 读取数据"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e827e4aa-375a-45a0-b6c6-b80bd637b79f",
   "metadata": {},
   "source": [
    "load方法来读取存储的数组，如果是.npz文件，读取之后相当于形成了一个类字典对象数据类型。通过保存时定义的key来获取相应的值ndarray"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "bcc9f355-1bd9-4bde-9130-81aedb0db77c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  1.        ,   3.02040816,   5.04081633,   7.06122449,\n",
       "          9.08163265,  11.10204082,  13.12244898,  15.14285714,\n",
       "         17.16326531,  19.18367347],\n",
       "       [ 21.20408163,  23.2244898 ,  25.24489796,  27.26530612,\n",
       "         29.28571429,  31.30612245,  33.32653061,  35.34693878,\n",
       "         37.36734694,  39.3877551 ],\n",
       "       [ 41.40816327,  43.42857143,  45.44897959,  47.46938776,\n",
       "         49.48979592,  51.51020408,  53.53061224,  55.55102041,\n",
       "         57.57142857,  59.59183673],\n",
       "       [ 61.6122449 ,  63.63265306,  65.65306122,  67.67346939,\n",
       "         69.69387755,  71.71428571,  73.73469388,  75.75510204,\n",
       "         77.7755102 ,  79.79591837],\n",
       "       [ 81.81632653,  83.83673469,  85.85714286,  87.87755102,\n",
       "         89.89795918,  91.91836735,  93.93877551,  95.95918367,\n",
       "         97.97959184, 100.        ]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new1 = np.load('data_n1.npy')\n",
    "new1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "fdd40313-d7d3-42dd-8a80-e6e73fcc086b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "NpzFile 'data_zip_n1_n2.npz' with keys: n1, n2"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new2 = np.load('data_zip_n1_n2.npz')\n",
    "new2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "bca4cdd2-d4e8-451c-bc63-050733601d8a",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  1.        ,   3.02040816,   5.04081633,   7.06122449,\n",
       "          9.08163265,  11.10204082,  13.12244898,  15.14285714,\n",
       "         17.16326531,  19.18367347],\n",
       "       [ 21.20408163,  23.2244898 ,  25.24489796,  27.26530612,\n",
       "         29.28571429,  31.30612245,  33.32653061,  35.34693878,\n",
       "         37.36734694,  39.3877551 ],\n",
       "       [ 41.40816327,  43.42857143,  45.44897959,  47.46938776,\n",
       "         49.48979592,  51.51020408,  53.53061224,  55.55102041,\n",
       "         57.57142857,  59.59183673],\n",
       "       [ 61.6122449 ,  63.63265306,  65.65306122,  67.67346939,\n",
       "         69.69387755,  71.71428571,  73.73469388,  75.75510204,\n",
       "         77.7755102 ,  79.79591837],\n",
       "       [ 81.81632653,  83.83673469,  85.85714286,  87.87755102,\n",
       "         89.89795918,  91.91836735,  93.93877551,  95.95918367,\n",
       "         97.97959184, 100.        ]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new2_n1 = new2['n1']\n",
    "new2_n1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "a169d886-15ee-4fcc-ba56-14bddb65d75e",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122,\n",
       "         124, 126, 128, 130, 132, 134, 136, 138, 140, 142, 144, 146,\n",
       "         148, 150, 152, 154, 156, 158],\n",
       "        [160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180, 182,\n",
       "         184, 186, 188, 190, 192, 194, 196, 198, 200, 202, 204, 206,\n",
       "         208, 210, 212, 214, 216, 218],\n",
       "        [220, 222, 224, 226, 228, 230, 232, 234, 236, 238, 240, 242,\n",
       "         244, 246, 248, 250, 252, 254, 256, 258, 260, 262, 264, 266,\n",
       "         268, 270, 272, 274, 276, 278],\n",
       "        [280, 282, 284, 286, 288, 290, 292, 294, 296, 298, 300, 302,\n",
       "         304, 306, 308, 310, 312, 314, 316, 318, 320, 322, 324, 326,\n",
       "         328, 330, 332, 334, 336, 338],\n",
       "        [340, 342, 344, 346, 348, 350, 352, 354, 356, 358, 360, 362,\n",
       "         364, 366, 368, 370, 372, 374, 376, 378, 380, 382, 384, 386,\n",
       "         388, 390, 392, 394, 396, 398]],\n",
       "\n",
       "       [[400, 402, 404, 406, 408, 410, 412, 414, 416, 418, 420, 422,\n",
       "         424, 426, 428, 430, 432, 434, 436, 438, 440, 442, 444, 446,\n",
       "         448, 450, 452, 454, 456, 458],\n",
       "        [460, 462, 464, 466, 468, 470, 472, 474, 476, 478, 480, 482,\n",
       "         484, 486, 488, 490, 492, 494, 496, 498, 500, 502, 504, 506,\n",
       "         508, 510, 512, 514, 516, 518],\n",
       "        [520, 522, 524, 526, 528, 530, 532, 534, 536, 538, 540, 542,\n",
       "         544, 546, 548, 550, 552, 554, 556, 558, 560, 562, 564, 566,\n",
       "         568, 570, 572, 574, 576, 578],\n",
       "        [580, 582, 584, 586, 588, 590, 592, 594, 596, 598, 600, 602,\n",
       "         604, 606, 608, 610, 612, 614, 616, 618, 620, 622, 624, 626,\n",
       "         628, 630, 632, 634, 636, 638],\n",
       "        [640, 642, 644, 646, 648, 650, 652, 654, 656, 658, 660, 662,\n",
       "         664, 666, 668, 670, 672, 674, 676, 678, 680, 682, 684, 686,\n",
       "         688, 690, 692, 694, 696, 698]],\n",
       "\n",
       "       [[700, 702, 704, 706, 708, 710, 712, 714, 716, 718, 720, 722,\n",
       "         724, 726, 728, 730, 732, 734, 736, 738, 740, 742, 744, 746,\n",
       "         748, 750, 752, 754, 756, 758],\n",
       "        [760, 762, 764, 766, 768, 770, 772, 774, 776, 778, 780, 782,\n",
       "         784, 786, 788, 790, 792, 794, 796, 798, 800, 802, 804, 806,\n",
       "         808, 810, 812, 814, 816, 818],\n",
       "        [820, 822, 824, 826, 828, 830, 832, 834, 836, 838, 840, 842,\n",
       "         844, 846, 848, 850, 852, 854, 856, 858, 860, 862, 864, 866,\n",
       "         868, 870, 872, 874, 876, 878],\n",
       "        [880, 882, 884, 886, 888, 890, 892, 894, 896, 898, 900, 902,\n",
       "         904, 906, 908, 910, 912, 914, 916, 918, 920, 922, 924, 926,\n",
       "         928, 930, 932, 934, 936, 938],\n",
       "        [940, 942, 944, 946, 948, 950, 952, 954, 956, 958, 960, 962,\n",
       "         964, 966, 968, 970, 972, 974, 976, 978, 980, 982, 984, 986,\n",
       "         988, 990, 992, 994, 996, 998]]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new2_n2 = new2['n2']\n",
    "new2_n2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57173e04-d981-480a-bd92-302be5a6e66a",
   "metadata": {},
   "source": [
    "## 读取CSV、text文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "9282dee8-8c57-4738-b2e2-1bd163cd2911",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 5, 2, 4],\n",
       "       [4, 3, 4, 1],\n",
       "       [2, 2, 0, 1]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.random.randint(0, 10, size=(3,4))\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "84ffb03d-97c2-44c3-8879-2aaefe40cd98",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.savetxt('text_arr.txt', arr, delimiter=',')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "9d78416f-a79e-421d-893c-2106162dba05",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.savetxt('csv_arr.csv', arr, delimiter=',')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "ac7f2a48-9fcb-4e6e-b5e8-68994770cf2c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 5., 2., 4.],\n",
       "       [4., 3., 4., 1.],\n",
       "       [2., 2., 0., 1.]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 读取txt文件\n",
    "new_arr = np.loadtxt('csv_arr.csv', delimiter=',')\n",
    "new_arr   # 他变成了浮点数了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "d5c00049-5c2d-4627-b654-ad787d9fbcb1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float64')"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_arr.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "0d117032-9490-4419-b047-d11cbcbe70c6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 现在我们将它变换数据类型\n",
    "new_arr = new_arr.astype(np.int32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "49ae6911-92d6-4c2e-9007-c67c7cca2583",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 5, 2, 4],\n",
       "       [4, 3, 4, 1],\n",
       "       [2, 2, 0, 1]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "99bdba7d-8063-4100-8d3d-75cf31a2c2d6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
